#include "goalie.h"

Goalie::Goalie(string teamName) : Player(teamName)
{ 
    ss.str("");
    ss<<"(init "<<teamName <<" (goalie))\n";
    sock << ss.str();
    ss.str(string());
    string response;
    sock >> response;
    sock.setReceiverAdress();
}


void Goalie::catchBall()
{
    int maxangle=2;
    int turnangle=1;

    get_status();

    if(parser.goal)
    {
        shippingbox.str("");
        shippingbox << "(move -50 0)\n";
        sock<<shippingbox.str();
    }

    if(parser.objectball)
    {
        if(parser.goal_kick)
        {
            if(parser.ball[0]>1)
                run(20);
            else
            {
                kick(60,parser.flag_c[1]);
                parser.goal_kick=0;
            }
        }

        if (parser.ball[1]<maxangle && parser.ball[1]>-maxangle && parser.ball[0]<=1){
            //cout <<"Catching ball"<<endl;
            shippingbox.str("");
            shippingbox<<"(catch "<<parser.ball[1]<<")\n"; //parser.ball[1] --> direccion del balón??
            sock<<shippingbox.str();
        }

        if (parser.goalie_catch_ball || parser.ball[0]<=1)
        {
            kick(80,parser.flag_c[1]);
        }
        else if (parser.ball[0]<5 && parser.ball[0]>2)
            run(30);
        else if (parser.ball[1]>maxangle)
            turn (turnangle);
        else if (parser.ball[1]<-maxangle)
            turn (-turnangle);


    }
    else if(!parser.objectball)
    {
        if (parser.goal && parser.flag_c[1]>20)// && parser.flag_c[1]>-maxangle)
            turn (3*turnangle);
        if (parser.goal && parser.ball[1]<-20)
            turn (-3*turnangle);
        else
            turn(turnangle*10);
    }
}



void Goalie::run(double power) //(dash power)
{
    shippingbox.str("");
    shippingbox << "(dash "<< power <<")\n";
    sock<<shippingbox.str();

}

void Goalie::kick(int power,int direction) //(kick Power Direction)
{
    shippingbox.str("");
    shippingbox << "(kick "<< power <<" "<< direction<<")\n";
    sock<<shippingbox.str();
}

void Goalie::turn(int moment) //(turn Moment)
{  
    shippingbox.str("");
    shippingbox << "(turn "<< moment <<")\n";
    sock<<shippingbox.str();
}

void Goalie::turn_neck(int angle) //(turn_neck Angle)
{
    shippingbox.str("");
    shippingbox << "(turn_neck "<< angle <<")\n";
    shippingbox.str("");
}


void Goalie::catch_dir(int dir)//(catch Direction)
{
    shippingbox.str("");
    shippingbox << "(catch "<< dir <<")\n";
}



